<?php
namespace Home\Controller;
use Think\Controller;
class DegxController extends Controller {
	/**
	 * [index 主页面]
	 * @return [type] [如果有session则显示，否则显示登录页面]
	 */

    public function index(){
        $this->display();
    }

    public function read()
    {
        $degx=M("degx");

        $pagenum=isset($_POST['page']) ? intval($_POST['page']) : 1;
        $rowsnum=isset($_POST['rows']) ? intval($_POST['rows']) : 10;

        /* 搜索条件 start*/
        $map=array();
        // 获取表所有字段名
        $Fields=$degx->getDbFields();
        foreach ($Fields as $key => $value) {
            if(isset($_POST[$value]) && $_POST[$value]!=""){
                $datas=$_POST[$value];
                if($value!='createtime')
                    $map[$value]=array('LIKE',array("%".$datas."%"));
            }
        }
        $createtimes=isset($_POST['createtimes']) ? $_POST['createtimes'] : '';
        $createtimee=isset($_POST['createtimee']) ? $_POST['createtimee'].' 23:59:59' : '';

        if($createtimes!="" and $createtimee!=""){
            $map['unix_timestamp(`createtime`)']= array(array('egt',strtotime($createtimes)),array('elt',strtotime($createtimee)),'AND');
        }


        if($createtimes!="" and $createtimee==""){

            $map['unix_timestamp(`createtime`)']=array('EGT',strtotime($createtimes));
        }

        if($createtimes=="" and $createtimee!=""){

            $map['unix_timestamp(`createtime`)']=array('ELT',strtotime($createtimee));
        }

// $map['createtime'] = array(array('egt',strtotime($createtimes)),array('elt',strtotime($createtimee)));

        // 将map写入session
//        session('map_degx',$map);
        /* 搜索条件 end*/
        $degxlist=$degx->where($map)->limit(($pagenum-1)*$rowsnum.','.$rowsnum)->order('id desc')->select();

        $total=$degx->where($map)->count();
        if ($total==0){
            $json='{"total":'.$total.',"rows":[]}';
            echo $json;
        }else{
            $json='{"total":'.$total.',"rows":'.json_encode($degxlist).'}';//重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
            echo $json;
        }
    }
    public function showadd(){
        $this->display();
    }


    public function add(){
        $degx= M("degx");
        $datas=$_POST;
            if(!empty($_FILES)){
                $config = array(
                    'rootPath' => './Public/',  //根目录
                    'savePath'  => 'upload/degx/', //保存路径
                );
                $upload = new \Think\Upload($config);
                $z =$upload->upload();
                if(!$z){
                    $datas['pics']="[]";
                    $datas['fjs']="[]";
                }else {
                    foreach ($z as $key=>$vaule){

                        $pics[$key]['name']=$z[$key]['pics']['name'];
                        $pics[$key]['savename']=$z[$key]['pics']['savepath'].$z[$key]['savename'];
                        $fjs[$key]['name']=$z[$key]['fjs']['name'];
                        $fjs[$key]['savename']=$z[$key]['fjs']['savepath'].$z[$key]['savename'];
                    }
                    $datas['pics'] =html_entity_decode(json_encode($pics));
                    $datas['fjs'] =html_entity_decode(json_encode($fjs));

                    $data['uid']=session('uid');
                    $data['nickname']=session('unickname');


                    //把已经上传好的图片制作缩略图Image.class.php
//                    $image = new \Think\Image();
                }
            }
            $datas['createtime']=date("Y-m-d",time());

            $z = $degx-> add($datas);
        if ($z) {
            echo json_encode(array("status" => 1, "msg" => '添加工序成功！', 'id' => $z));
        } else {
            echo json_encode(array("status" => 0, "msg" => '添加工序失败！'));
        }
    }


    /**
     * 分类数据
     */
    public function fldata()
    {
        $fl=M("fl");

        $pagenum=isset($_POST['page']) ? intval($_POST['page']) : 1;
        $rowsnum=isset($_POST['rows']) ? intval($_POST['rows']) : 10;

        /* 搜索条件 start*/
        $map=array();
        // 获取表所有字段名
        $Fields=$fl->getDbFields();
        foreach ($Fields as $key => $value) {
            if(isset($_POST[$value])){
                $datas=$_POST[$value];
                $map[$value]=array('LIKE',array("%".$datas."%"));
            }
        }

// 日期区间搜索
        $dates=isset($_POST['dates']) ? $_POST['dates'] : '';
        $datee=isset($_POST['datee']) ? $_POST['datee'].' 23:59:59' : '';

        if($dates!="" and $datee!=""){
            $map['unix_timestamp(`createtime`)']= array(array('egt',strtotime($dates)),array('elt',strtotime($datee)),'AND');
        }


        if($dates!="" and $datee==""){

            $map['unix_timestamp(`createtime`)']=array('EGT',strtotime($dates));
        }

        if($dates=="" and $datee!=""){

            $map['unix_timestamp(`createtime`)']=array('ELT',strtotime($datee));
        }

        // 将map写入session
        session('map_fl',$map);
        /* 搜索条件 end*/
//        $map['lb']=$_POST['lb'];
        $fllist=$fl->where($map)->limit(($pagenum-1)*$rowsnum.','.$rowsnum)->order('createtime desc')->select();

        $total=$fl->where($map)->count();
        if ($total==0){
            $json='{"total":'.$total.',"rows":[]}';
            echo $json;
        }else{
            $json='{"total":'.$total.',"rows":'.json_encode($fllist).'}';//重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
            echo $json;
        }
    }

    /**
     * 存储分类
     */
    public function savefl()
    {
        $fl=M('fl');
        $data=$_POST;
        $data['createtime'] = date("Y/m/d H:i:s");
        $result = $fl->add($data);
        if ($result) {
            echo json_encode(array("status" => 1, "msg" => '添加分类组合项成功！', 'id' => $result));
        } else {
            echo json_encode(array("status" => 0, "msg" => '添加分类组合项失败！'));
        }

    }

    public function cxfl()
    {
        $cxfl=M("cxfl");
        $fl=$_POST['fl'];
        $map['fl']=$fl;
        $data=$cxfl->where($map)->order('groups')->select();
        echo json_encode($data);
    }



    public function ffdw(){
        $data=array();
        $data[0]['value']='装配车间';
        $data[0]['text']='装配车间';
        $data[1]['value']='试制车间';
        $data[1]['text']='试制车间';
        $data[2]['value']='内饰车间';
        $data[2]['text']='内饰车间';
        $data[3]['value']='调试车间';
        $data[3]['text']='调试车间';
        $data[4]['value']='质检科';
        $data[4]['text']='质检科';
        $data[5]['value']='物料科';
        $data[5]['text']='物料科';
        $data[6]['value']='生产科';
        $data[6]['text']='生产科';
        echo json_encode($data);


    }


 
    public function del(){

        $degx=M("degx");
        $degxdata=M("degxdata");
        $map['id']= $_POST['id'];
        $maps['ggdid']= $_POST['id'];
        $state=$degx->where($map)->getField('state');
        if($state!=1 ){
            echo json_encode(array("status" => 0,"msg" =>'已经发布的临时更改单无法删除！'));
        }else{
            $r=$degx->where($map)->delete();
            $degxdata->where($maps)->delete();

            if($r){
                $data['status']=1;
                $data['msg']="删除临时更改单成功！";
                echo json_encode($data);
            }else{
                echo json_encode(array("status" => 0,"msg" =>'删除作业指导书失败！'));
            }
        }

    }
}